package com.afsadmedia.pregnancy;

import java.util.ArrayList;
import java.util.List;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.FrameLayout.LayoutParams;

public class Measurement extends SherlockActivity {

	List<ListItemWeight> rowItems;
	CustomListViewAdapterWeightMang adapter;

	ArrayList<String> adddate, addweight;

	ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.measurement);

		rowItems = new ArrayList<ListItemWeight>();
		for (int i = 0; i < Constants.addedbodypart.size(); i++) {

			ListItemWeight item1 = new ListItemWeight(
					Constants.addedbodypart.get(i),
					Constants.addedmeasurement.get(i));

			rowItems.add(item1);

		}
	

		listView = (ListView) findViewById(R.id.listView_measurements);

		adapter = new CustomListViewAdapterWeightMang(getApplicationContext(),
				R.layout.listitem_weightmang, rowItems);
		listView.setAdapter(adapter);
	}

	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		getSupportActionBar().setBackgroundDrawable(
				getResources().getDrawable(R.drawable.actionbar_background));
		getSupportActionBar().setTitle("Favorite");
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setHomeButtonEnabled(true);

		menu.add(Menu.NONE, R.id.add, Menu.NONE, R.string.toggle_show_home)
				.setIcon(R.drawable.addicon)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

		return super.onCreateOptionsMenu(menu);

	}

	public boolean onOptionsItemSelected(
			com.actionbarsherlock.view.MenuItem item) {
		switch (item.getItemId()) {

		case android.R.id.home:

			// Intent in=new Intent(Details.this,FavItems.class);
			// in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			// startActivity(in);

			return true;

		case R.id.add:

			showPopUP();
			return true;
		default:
			return false;
		}
	}

	private void showPopUP() {
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setCancelable(false);
		alert.setTitle("Add Measurements");

		LinearLayout layout2 = new LinearLayout(Measurement.this);
		layout2.setOrientation(LinearLayout.VERTICAL);

		// ///////
		final EditText input = new EditText(this);
		input.setWidth(140);
		input.setHint("Body Part");

		// ////////
		final EditText input1 = new EditText(this);
		InputFilter[] FilterArray = new InputFilter[1];
		FilterArray[0] = new InputFilter.LengthFilter(3);
		input1.setFilters(FilterArray);
		input1.setInputType(InputType.TYPE_CLASS_NUMBER);
		input1.setWidth(90);
		input1.setHint("Number");

		// ///////
		final TextView textView = new TextView(this);
		textView.setTextSize(20);
		textView.setTextColor(Color.BLACK);
		textView.setText("  Cm ");

		// ///////
		final LinearLayout layout22 = new LinearLayout(Measurement.this);
		layout22.setOrientation(LinearLayout.HORIZONTAL);
		layout22.setPadding(1, 20, 1, 5);
		layout22.setBackgroundColor(Color.WHITE);
		layout22.setOrientation(LinearLayout.HORIZONTAL);
		layout22.addView(input);
		layout22.addView(input1);
		layout22.addView(textView);

		// //////
		final LinearLayout layout33 = new LinearLayout(Measurement.this);
		layout33.setOrientation(LinearLayout.HORIZONTAL);
		layout33.setGravity(Gravity.CENTER_HORIZONTAL);

		// /////
		Button button = new Button(Measurement.this);
		button.setText("Cancel");

		// ///////
		Button button2 = new Button(Measurement.this);
		button2.setText("Ok");

		layout33.addView(button);
		layout33.addView(button2);

		layout2.addView(layout22);
		layout2.addView(layout33);
		alert.setView(layout2);

		// ////////////////////////////////////////////

		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (input.getText().toString().equals("")
						|| input1.getText().toString().equals("")) {
					Toast.makeText(getApplicationContext(),
							"Please Enter the Data", Toast.LENGTH_LONG).show();
				} else {

					Constants.selectbodypart = input.getText().toString();
					Constants.selectmeasurement = input1.getText().toString()
							+ " Cm ";

					Log.v("ASI", Constants.selectbodypart);

					Constants.addedbodypart.add(Constants.selectbodypart);
					Constants.addedmeasurement.add(Constants.selectmeasurement);

					Intent i = new Intent(getApplicationContext(),
							Measurement.class);
					startActivity(i);
					finish();

				}

			}
		});

		AlertDialog alert123 = alert.create();
		alert123.show();

	}

}
